<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <jsp:include page="../../../../../head.jsp"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户表</title>
</head>
<script type="text/javascript">
    var roleId = "${id}";
    $(function () {
        initTree("company-listCompany", null);
    });

    function ajaxDataFilter(treeId, parentNode, responseData) {
        if (!responseData.depts && !responseData.users) {
            return null;
        } else if (responseData.depts && !responseData.users) {
            return responseData.depts;
        } else if (!responseData.depts && responseData.users) {
            return responseData.users;
        }
    }
    ;

    function zTreeOnClick(event, treeId, treeNode) {
        var level = treeNode.level;
        if (level == 1) {
            if (treeNode.zAsync) {
                var url = "role-listRoleUser?dept.id=" + treeNode.id + "&role.id=" + roleId;
                $.ajax({
                           url: url,
                           async: true,
                           type: "POST",
                           success: function (data) {
                               var treeObj = $.fn.zTree.getZTreeObj("tree");
                               treeObj.addNodes(treeNode, data.users);
                               if (data.users) {
                                   $.each(data.users, function (i, n) {
                                       if (n.checked) {
                                           setParentNodeSelected(treeNode);
                                           treeObj.refresh();
                                           return false;
                                       }
                                   });
                               }
                           }
                       });
                treeNode.zAsync = false;
            }
        }
    }
    ;

    /*循环找出节点的父节点，并且打上勾*/
    function setParentNodeSelected(treeNode) {
        treeNode.checked = true;
        var parentNode = treeNode.getParentNode();
        if (parentNode) {
            setParentNodeSelected(parentNode);
        } else {
            return;
        }
    }

    var setting = {
        check: {
            enable: true
        },
        async: {
            enable: true,
            url: "company-listDept",
            type: "post",
            dataType: "json",
            autoParam: ["id=id"],
            dataFilter: ajaxDataFilter
        },
        callback: {
            onClick: zTreeOnClick
        }
    };

    /**初始化公司树*/
    function initTree(src, params) {
        $.post(src, params, function (value) {
            if (!value)return;
            $.fn.zTree.init($("#tree"), setting, value);
        });
    }

    function refresh() {
        zTree.reAsyncChildNodes(null, "refresh");
    }

    function save() {
        if (roleId) {
            var zTree = $.fn.zTree.getZTreeObj("tree");
            var nodes = zTree.getNodesByFilter(function (node) {
                if (node.level == 2) {
                    return true;
                }
            }, false);
            submitTree(nodes, "role-saveRoleUser", {
                "role.users{n}.id": "id",
                "role.users{n}.checked": "checked"
            }, {"id": roleId}, "n", function (data, textStatus, jqXHR) {
                tree_refresh(zTree);
                if (data.result) {
                    jw.alert(data.msg);
                }
            });
        } else {
            alert("保存出错，请联系管理员！");
        }
    }
</script>
<body>

<ul class="buttons">
    <li><input id="saveButton" type="button" onclick="save()"
               value="保存"/></li>
</ul>
<ul id="tree" class="ztree" style="width:230px; overflow:auto;"></ul>
</body>
</html>
